package cn.tedu.executorservice;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduledExecutorServiceDemo {
    public static void main(String[] args) {
        ScheduledExecutorService ses = Executors.newScheduledThreadPool(5);
        // 推迟5秒执行线程
//        ses.schedule(new ScheduleThread(), 5, TimeUnit.SECONDS);

        // 从上一次启动开始, 来计算下一次启动的时间, 每隔5秒执行一次
        // 如果线程的执行时间超过间隔时间, 则以线程的执行时间为准
        ses.scheduleAtFixedRate(new ScheduleThread(), 0, 5, TimeUnit.SECONDS);

        // 从上一次结束开始, 来计算下一次的启动时间, 每隔5秒执行一次
//        ses.scheduleWithFixedDelay(new ScheduleThread(), 0, 5, TimeUnit.SECONDS);
    }
}

class ScheduleThread implements Runnable {
    @Override
    public void run() {
        try {
            System.out.println("Hello");
            Thread.sleep(8000);
        } catch (InterruptedException interruptedException) {
            interruptedException.printStackTrace();
        }
    }
}
